package com.bro.person.admin.websocket;

import org.springframework.util.Assert;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author swallow
 * Created by swallow on 2018/10/9.<br>
 */
public class SocketSessionRegistry {

    private final ConcurrentMap<String,Set<String>> userSessionIds = new ConcurrentHashMap<>();

    private final Object lock = new Object();

    public SocketSessionRegistry() {
    }

    /**
     * 获取sessionId
     * @param user
     * @return
     */
    public Set<String> getSessionIds(String user){
        Set set = (Set)this.userSessionIds.get(user);
        return set!=null?set: Collections.emptySet();
    }

    /**
     * 获取所有的sessionId
     * @return
     */
    public ConcurrentMap<String,Set<String>> getAllSessionIds(){
        return this.userSessionIds;
    }


    /**
     * 注册session
     * @param user
     * @param sessionId
     */
    public void registerSessionId(String user,String sessionId){
        Assert.notNull(user,"user must not be null");
        Assert.notNull(sessionId,"Session ID must not be null");
        Object var3 = this.lock;
        synchronized (this.lock){
            Object set = (Set) this.userSessionIds.get(user);
            if(set == null){
                set = new CopyOnWriteArraySet();
                this.userSessionIds.put(user,(Set<String>) set);
            }

            //TODO 当最迟登陆的时间和当前时间的年月日不匹配的时候清空session缓存

            ((Set)set).add(sessionId);
        }
    }

    public void unregisterSessionId(String userName,String sessionId){
        Assert.notNull(userName,"User Name must not be null");
        Assert.notNull(sessionId,"Session ID must not be null");
        Object var3 = this.lock;
        synchronized (this.lock){
            Set set = (Set) this.userSessionIds.get(userName);
            if(set!=null && set.remove(sessionId) && set.isEmpty()){
                this.userSessionIds.remove(userName);
            }
        }
    }
}
